package com.gzbugu.bugusys.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.dao.IHQLQuery;


/**
 * HQL数据库查询
 * 
 * @author gauze
 * 
 */
@Transactional
public class HQLQuery extends HibernateDaoSupport implements IHQLQuery {

	/**
	 * 查询
	 * 
	 * @param hql
	 *            hql语句
	 * @param page
	 *            分页模型
	 * @return 查询结果
	 */
	@SuppressWarnings("unchecked")
	public List find(String hql, PageModel page) {
		return this.find(hql, (page.getCurrentPage() - 1) * page.getPageSize(),
				page.getPageSize());
	}

	/**
	 * 查询
	 * 
	 * @param hql
	 *            hql语句
	 * @param offset
	 *            开始位置
	 * @param pagesize
	 *            查询记录总数
	 * @return 查询结果
	 */
	@SuppressWarnings("unchecked")
	public List find(String hql, int offset, int pagesize) {
		try {
			Query query = this.getSession().createQuery(hql);
			query.setFirstResult(offset);
			query.setMaxResults(pagesize);
			return query.list();
		} catch (Exception e) {
			return new ArrayList();
		}
	}

	/**
	 * 查询
	 * 
	 * @param hql
	 *            查询hql语句
	 * @return 查询结果
	 */
	@SuppressWarnings("unchecked")
	public List find(String hql) {
		try {
			Query query = this.getSession().createQuery(hql);
			return query.list();
		} catch (Exception e) {
			return new ArrayList();
		}
	}

	/**
	 * 查询,推荐使用此方法，可防止hql注入攻击
	 * 
	 * @param prepartHql
	 *            带有参数的hql语句
	 * @param paramsMap
	 *            保存有参数名和值的map
	 * @return 查询结果
	 */
	@SuppressWarnings("unchecked")
	public List find(String prepartHql, Map<String, Object> paramsMap) {
		try {
			Query query = this.getSession().createQuery(prepartHql);
			Set<Map.Entry<String, Object>> set = paramsMap.entrySet();
			Iterator<Entry<String, Object>> iter = set.iterator();
			while (iter.hasNext()) {
				Map.Entry<String, Object> entry = iter.next();
				String key = entry.getKey();
				Object value = entry.getValue();
				query.setParameter(key, value);
			}
			return query.list();
		} catch (DataAccessResourceFailureException e) {
			return new ArrayList();
		} catch (HibernateException e) {
			return new ArrayList();
		} catch (IllegalStateException e) {
			return new ArrayList();
		}
	}

	/**
	 * 查询,推荐使用此方法，可防止hql注入攻击
	 * 
	 * @param prepartHql
	 *            带有参数的hql语句
	 * @param paramsMap
	 *            保存有参数名和值的map
	 * @param offset
	 *            开始位置
	 * @param pagesize
	 *            查询记录数
	 * @return 查询结果
	 */
	@SuppressWarnings("unchecked")
	public List find(String prepartHql, Map<String, Object> paramsMap,
			int offset, int pagesize) {
		try {
			Query query = this.getSession().createQuery(prepartHql);
			Set<Map.Entry<String, Object>> set = paramsMap.entrySet();
			Iterator<Entry<String, Object>> iter = set.iterator();
			while (iter.hasNext()) {
				Map.Entry<String, Object> entry = iter.next();
				String key = entry.getKey();
				Object value = entry.getValue();
				query.setParameter(key, value);
			}
			query.setFirstResult(offset);
			query.setMaxResults(pagesize);
			return query.list();
		} catch (DataAccessResourceFailureException e) {
			return new ArrayList();
		} catch (HibernateException e) {
			return new ArrayList();
		} catch (IllegalStateException e) {
			return new ArrayList();
		}
	}

	/**
	 * 查询,推荐使用此方法，可防止hql注入攻击
	 * 
	 * @param prepartHql
	 *            带有参数的hql语句
	 * @param paramsMap
	 *            保存有参数名和值的map
	 * @param page
	 *            分页模型
	 * @return 查询结果
	 */
	@SuppressWarnings("unchecked")
	public List find(String prepartHql, Map<String, Object> paramsMap,
			PageModel page) {
		return this.find(prepartHql, paramsMap, (page.getCurrentPage() - 1)
				* page.getPageSize(), page.getPageSize());
	}
}
